import * as equipmentService from '../../services/equipment';
import {queryAssetType} from '../../services/assetType';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryAllRoomList} from '../../services/tRoom';
import {queryAllContractList} from '../../services/contract';
export default {
  namespace: 'preset',
  state: {
    current: 1,
    currentSize: 10,
    total: 0,
    presetId:'',
    assetTypeTree:[],
    list: [],
    roomList:[],
    contractList:[],
    modalVisible : false,
    modalType : 'create',
    detailList:[],
    assetBrandList:[],
    assetModelList:[],
    modalDetailVisible:false,
    detailModalType : 'create',
    detailInfoModalType : 'createDetail',
    modalDetailInfoVisible:false,
    importModalVisible:false,
    modalViewVisible:false,
    currentInfoItem:{},
    currentItem:{},
    errorMsg:'',
    newKey:'',
    newKey1:'',
    newKey2:'',
    newKey3:'',
    newKey4:'',
  },
  reducers: {
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
        return { ...state, assetTypeTree };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryModelListSuc(state, { payload: assetModelList }) {
      return { ...state, assetModelList };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
        return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
    queryPresetListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryContractListSuc(state, { payload: contractList }) {
      return { ...state, contractList };
    },
    showDetailModal (state, { payload }) {
        return { ...state, ...payload, modalDetailVisible: true }
    },
    hideDetailModal (state) {
        return { ...state, modalDetailVisible: false,}
    },
    showDetailInfoModal (state, { payload }) {
        return { ...state, ...payload, newKey2 : new Date().getTime() + '' , modalDetailInfoVisible: true, }
    },
    hideDetailInfoModal (state) {
        return { ...state, modalDetailInfoVisible: false, newKey2 : new Date().getTime() + '',currentInfoItem: {} }
    },
    showViewModal (state, { payload }) {
        return { ...state, ...payload, newKey4 : new Date().getTime() + '' , modalViewVisible: true, }
    },
    hideViewModal (state) {
        return { ...state, modalViewVisible: false, newKey4 : new Date().getTime() + '',currentInfoItem: {} }
    },

    showExportModal (state, { payload }) {
        return { ...state, ...payload, newKey3 : new Date().getTime() + '' , importModalVisible: true, }
    },
    hideExportModal (state) {
        return { ...state, importModalVisible: false, newKey3 : new Date().getTime() + '',currentInfoItem: {} }
    },

  },
  effects: {
    *create({ payload },{ call, put }) {
      const { body }  = yield call(equipmentService.create,payload);
      yield put({ type: 'reload',payload });
      yield put({ type: 'hideModal' });
    },

    *qryById({ payload },{ call, put }){
      const { body } = yield call(equipmentService.qryById,payload);
      yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            title: '编辑清单',
          },
      });
    },

   *edit({ payload },{ call, put }) {
      const { body }  = yield call(equipmentService.edit,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload',payload });

    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },

   *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryContract({ payload },{ call, put }) {
      const { body } = yield call(queryAllContractList,payload);
      yield put({ type: 'queryContractListSuc', payload: body.data });
    },

    *queryPreset({ payload },{ call, put }) {
      const { body } = yield call(equipmentService.queryPresetList,payload);
      yield put({ type: 'queryPresetListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, datacenterId:payload.datacenterId }});
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(equipmentService.remove,payload.id);
      yield put({ type: 'reload', payload});
    },

  *viewDetail({ payload },{ call, put }){
      const { body } = yield call(equipmentService.queryDetailList,payload);
      yield put({
          type: 'showViewModal',
          payload: {
            currentItem: body.obj,
            detailList: body.data,
          },
      });
    },

    *qryDetailById({ payload },{ call, put }){
      const { body } = yield call(equipmentService.queryDetailList,payload);
      yield put({
          type: 'showDetailModal',
          payload: {
            currentItem: body.obj,
            presetId:body.obj.id_,
            detailList: body.data,
            detailModalType: 'edit',
            title: '查看清单详情',
          },
      });
    },

    *delDetail({ payload },{ call, put }){
      const { body } = yield call(equipmentService.delDetail,payload.id);
      yield put({ type: 'qryDetailById', payload:payload.presetId});
    },

    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'queryModelListSuc', payload: body.data });
    },

    *editDetail({ payload,form },{ call, put }) {
        const { body }  = yield call(equipmentService.editDetail,payload.data);
        if(body.httpCode==200){
          yield put({ type: 'qryDetailById', payload:payload.presetId});
          form.resetFields();
          yield put({ type: 'hideDetailInfoModal', payload});
        }
      },
    *createDetail({ payload,form },{ call, put }) {
      const { body }  = yield call(equipmentService.createDetail,payload.data);
      if(body.httpCode==200){
        yield put({ type: 'qryDetailById', payload:payload.presetId});
        form.resetFields();
        yield put({ type: 'hideDetailInfoModal', payload});
      }
    },

    *createDetailInfoModal({ payload },{ call, put }) {
      yield put({ type: 'showDetailInfoModal', payload:{...payload,detailInfoModalType: 'createDetail',}});
      yield put({ type: 'queryAssetType', payload:{}});
      yield put({ type: 'queryAssetBrand', payload:{}});

    },
   *qryDetailInfoById({ payload },{ call, put }){
      const { body } = yield call(equipmentService.qryPresetDetailInfoById,payload);
      yield put({
          type: 'showDetailInfoModal',
          payload: {
            currentInfoItem: body.data,
            detailInfoModalType: 'editDetail',
            title: '编辑清单详情',
          },
      });
      yield put({ type: 'queryAssetType', payload:{}});
      yield put({ type: 'queryAssetBrand', payload:{}});
      yield put({ type: 'queryAssetModel', payload:{typeId:body.data.typeId_,brandId:body.data.brandId_}});
    },

    *importExcelModalAndSelect({ payload },{ call, put }) {
        yield put({ type: 'hideExportModal'});
        yield put({ type: 'qryDetailById', payload:payload.presetId});
    },

    *reload({ payload },{ call, put }) {
        yield put({ type: 'queryPreset', payload});
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/equipment/preset') {
          dispatch({ type: 'queryRoom'});
          dispatch({ type: 'queryContract'});
          dispatch({ type: 'queryPreset', payload:{}});
        }
      });
    },
  },
};
